package cn.fzkj.ailangchain4j.controller;

import cn.fzkj.ailangchain4j.ai.CodeHelperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @ description
 * @ author yaya
 * @ since 2025/11/4
 */
@RestController
@RequestMapping("/ai")
public class AiController {

    @Autowired
    private CodeHelperService codeHelperService;

    @GetMapping("/chat")
    public Flux<ServerSentEvent<String>> chat(@RequestParam("memoryId") int memoryId, @RequestParam("message") String message) {
       return codeHelperService.chatStream(memoryId, message)
                .map(trunk -> {
            return ServerSentEvent.<String>builder().data(trunk).build();
        });
    }
}
